import axios from 'axios'
import router from '@/router/index'
import ElementUI from 'element-ui'
import store from '@/store/index'





let R = {
    data:null,
    code: '0',
    mgs: '前端统一提交数据格式'

}


/**
 * 实例一个axios对像
 */
const http = axios.create({
    timeout: 10000,
    headers: {
        'Content-Type': "application/json; charset=utf-8",
        'roleId': sessionStorage.getItem('roleId')
    }
})
/**
 * 统一请求地址
 */
http.defaults.baseURL = "http://localhost:88/api/"
// http.defaults.baseURL = "http://192.168.31.145:88/api/"

/**
 * 请求拦截器
 */
http.interceptors.request.use(config =>{

   store.commit("loadingStatus",true)
    return config
})

/**
 * 响应拦截器
 */
http.interceptors.response.use(response =>{
    store.commit("loadingStatus",false)
    // console.log("响应拦截，后置",response)
    //获取响应结果
    let res = response.data
    // console.log("响应结果",res)
    //判断状态码
    if (res.code == 200 || res.code == 0){
        return response
    }
    if (res.code==700){
        ElementUI.Message({
            message: res.msg,
        })
        return response
    }
    if (res.code==800){
        ElementUI.Message({
            message: res.msg,
            type: 'success'
        })
        return response
    }

    ElementUI.Message({
        message: res.msg,
        type: 'error'
            })
        return Promise.reject(response.data.mgs)
},
    error => {
        if (error.response.status ===401){
            // console.log("用户未登录或没有权限")
            router.push('login')
        }
    }
)
/**
 * 统一前端的返回值
 */
http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => {
   return data;
}
export default http

